package com.kermel.andromeda.activities.alarm.list;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.kermel.andromeda.R;
import com.kermel.andromeda.activities.alarm.add.AddAlarmActivity;
import com.kermel.andromeda.activities.alarm.view.AlarmView;
import com.kermel.andromeda.data.Alarm;
import com.kermel.andromeda.data.AlarmManager;

public class MyAlarmsListAdapter extends BaseAdapter implements OnLongClickListener{
    private Context context;
    private Alarm[] alarms;
    
    /**
     * Creates a new instance
     */
    public MyAlarmsListAdapter(Context context) {
        this.context = context;
        int numAlarms = getCount();
        alarms = new Alarm[numAlarms];
        AlarmManager.instance.getAlarms().toArray(alarms);
    }
    
    /** {@inheritDoc} */
    public int getCount() {
        return AlarmManager.instance.getAlarmCount();
    }

    /** {@inheritDoc} */
    public Object getItem(int position) {
        return alarms[position];
    }

    /** {@inheritDoc} */
    public long getItemId(int position) {
        long itemID = 0;
        Alarm alarm = alarms[position];
        if (alarm != null) {
            itemID = alarm.getUUID().getLeastSignificantBits();
        }
        return itemID;
    }

    /** {@inheritDoc} */
    public View getView(int position, View convertView, ViewGroup parent) {
        AlarmView alarmView = null;
        Alarm alarm = alarms[position];
        
        if (alarm != null) {
            if (convertView == null) {
                alarmView = new AlarmView(context, alarms[position]);
                alarmView.setOnLongClickListener(this);
            } else {
                alarmView = (AlarmView)convertView;
                alarmView.setAlarm(alarms[position]);
            }
        }
            
        return alarmView;
    }
    
    @Override
    public void notifyDataSetChanged() {
        int numAlarms = getCount();
        alarms = new Alarm[numAlarms];
        AlarmManager.instance.getAlarms().toArray(alarms);
        
        super.notifyDataSetChanged();
    }

    public boolean onLongClick(View view) {
        createChoicesAlert(view);
        return true;
    }
    
    public void createChoicesAlert(View view) {
        if (view != null && view instanceof AlarmView) {
            ChoicesAlert alert = new ChoicesAlert((AlarmView)view);
            alert.show();
        }
    }
    
    protected void editAlarm(Alarm alarm) {
        launchEditAlarmActivity(alarm);
    }
    
    private void launchEditAlarmActivity(Alarm alarm) {
        Intent intent = new Intent(context, AddAlarmActivity.class);
        intent.putExtra(Alarm.ALARM_PARCEL_NAME, alarm);
        context.startActivity(intent);
    }
    
    protected void deleteAlarm(Alarm alarm) {
        AlarmManager.instance.removeAlarm(alarm);
        notifyDataSetChanged();
    }
    
    private class ChoicesAlert implements DialogInterface.OnClickListener {
        protected static final int EDIT_ITEM = 0;
        protected static final int DELETE_ITEM = 1;
        
        private AlertDialog alert;
        private AlarmView alarmView;
        private Alarm alarm;
        
        public ChoicesAlert(AlarmView view) {
            alarmView = view;
            alarm = alarmView.getAlarm();
            
            AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
            builder.setTitle(alarm.name);
            
            final CharSequence[] choices = retrieveChoices();
            builder.setItems(choices, this);
            
            alert = builder.create();
        }
        
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case EDIT_ITEM:
                    editAlarm(alarm);
                    break;
    
                case DELETE_ITEM:
                    deleteAlarm(alarm);
                    break;
            }
        }
        
        public void show() {
            if (alert != null)
                alert.show();
        }
        
        private CharSequence[] retrieveChoices() {
            Resources resources = alarmView.getResources();
            TypedArray arrayItems = resources.obtainTypedArray(R.array.items);
            
            int numChoices = arrayItems.length();
            CharSequence[] choices = new CharSequence[numChoices];
            
            for (int i = 0; i < numChoices; i++) {
                choices[i] = arrayItems.getText(i);
            }
            
            arrayItems.recycle();
            
            return choices;
        }
        
    }

}
